<template>
  <div>
    <div style="padding: 10px">
      <el-row>
        <el-col span="6">
          用户名：
          <el-input v-model="text" @keyup.enter.native="load" style="width: 70%"> 
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </el-col>
       
        <el-col span="2">
          <el-button  type="primary" size="mini" >查找</el-button>
        </el-col>
        <el-col span="2">
          <el-button @click="add" type="primary" size="mini" >新增</el-button>
        </el-col>
      </el-row>
     
    </div>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="stuId" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="school" label="学校"></el-table-column>
      <el-table-column prop="grade" label="班级"></el-table-column>
     
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scope.row)"></el-button>
          <el-popconfirm
              @confirm="del(scope.row.stuId)"
              title="确定删除？"
          >
            <el-button type="danger" icon="el-icon-delete" circle slot="reference"
                       style="margin-left: 10px"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          :page-sizes="[2, 5, 10]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>

    <!-- 弹窗   -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <el-form :model="entity">
        <el-form-item label="用户名" label-width="150px">
          <el-input v-model="entity.username" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
       
        <el-form-item label="电话" label-width="150px">
          <el-input v-model="entity.phone" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
       
        <el-form-item label="学校" label-width="150px">
          <el-input v-model="entity.school" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>

        <el-form-item label="班级" label-width="150px">
          <el-input v-model="entity.grade" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>

        <el-form-item label="密码" label-width="150px">
          <el-input v-model="entity.password" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import API from '../../utils/request'

const url = "/api/user/"

export default {
  name: "User",
  data() {
    return {
      options: [],
      text: '',
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      entity: {},
      dialogFormVisible: false
    };
  },
  created() {
    this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    this.load()
  },
  methods: {
    changeRole(row) {
      this.entity = JSON.parse(JSON.stringify(row));
      this.save();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
      API.get('/admin/listStudents/'+this.pageNum+'/'+this.pageSize).then(res => {
        this.tableData = res.data.records || []
        this.total = res.data.total
      })

    
    },
    add() {
      this.entity = {}
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.stuId) {
        API.post('/admin/insertstudent', this.entity).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "操作成功",
              duration:800
            })
          } else {
            this.$message({
              type: "error",
              message: res.message
            })
          }
          this.load()
          this.dialogFormVisible = false
        })
      } else {
        API.post('/admin/updateStudent', this.entity).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "操作成功",
              duration:800
            })
          } else {
            this.$message({
              type: "error",
              message: res.message
            })
          }
          this.load()
          this.dialogFormVisible = false
        })
      }
    },
    del(id) {
      API.get('/admin/deleteStudents/'+id).then(res => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "操作成功",
            duration:800
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()
      })
    }
  },
};
</script>

<style scoped>
</style>
